import java.util.ArrayList;
import java.util.List;

/**
 * @program: Learning
 * @description: 测试list中，当某一列相同时将其他列拼接
 * @Company: 终生学习
 * @author: peter.wei
 * @create: 2020-08-03 14:35
 **/
public class TestContact {
    public static void main(String[] args) {
        List<A> oldList = new ArrayList<A>();//需要整合的List
        List<A> newList = new ArrayList<A>();//整合后的List
        A aA = new A();
        aA.setName("a");
        aA.setAge(10);
        A aB = new A();
        aB.setName("b");
        aB.setAge(20);
        A aC = new A();
        aC.setName("c");
        aC.setAge(30);
        A aD = new A();
        aD.setName("b");
        aD.setAge(40);

        oldList.add(aA);
        oldList.add(aB);
        oldList.add(aC);
        oldList.add(aD);
        for (A oldO : oldList) {
            boolean flag = true;//给整合后集合添加子元素标志，true：添加，false:不添加，其年龄相加
            for (A newO : newList) {
                if (newO.getName().equals(oldO.getName())) {//判断姓名是否相同
                    newO.setAge(newO.getAge() + oldO.getAge());//姓名相同时，年龄相加
                    flag = false;
                }
            }
            if (flag) {
                newList.add(oldO);//给整合后集合添加子元素
            }
        }

        for (A a : newList) {//打印结果
            System.out.println(a.getName() + " : " + a.getAge());
        }
    }
}

class A {
    private String name;//姓名
    private int age;//年龄

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}